package cn.com.blueInfo.bpm.system.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.system.entity.RoleResource;
import cn.com.blueInfo.bpm.system.enums.RoleResourcePermission;
import cn.com.blueInfo.bpm.system.mapper.RoleResourceMapper;
import cn.com.blueInfo.bpm.system.repository.RoleResourceRepository;
import cn.com.blueInfo.bpm.system.request.RoleResourceDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 角色-资源关联表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:17:41
 */
@Log4j2
@Service
public class RoleResourceRepositoryImpl
        extends ServiceImpl<RoleResourceMapper, RoleResource>
        implements RoleResourceRepository, BaseRepository<RoleResource, RoleResourceDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增角色-资源关联
     * @param roleResource 角色-资源关联参数
     */
    @Override
    public Integer createRoleResource(RoleResource roleResource) {
        return sqlExecuteResult(save(roleResource));
    }

    /**
     * 批量新增角色-资源关联
     * @param roleResourceList 角色-资源关联参数列表
     */
    @Override
    public Integer batchCreateRoleResource(List<RoleResource> roleResourceList) {
        return sqlExecuteResult(saveBatch(roleResourceList), roleResourceList.size());
    }

    /**
     * 删除角色-资源关联
     * @param roleResourceId 角色-资源关联ID
     */
    @Override
    public Integer deleteRoleResource(Long roleResourceId) {
        RoleResource roleResource = new RoleResource();
        roleResource.setId(roleResourceId);
        roleResource.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(roleResource));
    }

    /**
     * 批量删除角色-资源关联
     * @param roleResourceIdList 角色-资源关联ID列表
     */
    @Override
    public Integer batchDeleteRoleResource(List<Long> roleResourceIdList) {
        List<RoleResource> roleResourceList = new ArrayList<>();
        for (Long roleResourceId : roleResourceIdList) {
            RoleResource roleResource = new RoleResource();
            roleResource.setId(roleResourceId);
            roleResource.setDeleted(BaseDeleted.DELETED);
            roleResourceList.add(roleResource);
        }
        return sqlExecuteResult(removeBatchByIds(roleResourceList), roleResourceList.size());
    }

    /**
     * 更新部分角色-资源关联
     * @param roleResource 角色-资源关联参数
     */
    @Override
    public Integer updateRoleResourcePartial(RoleResource roleResource) {
        validateUpdateId(roleResource.getId());
        LambdaUpdateWrapper<RoleResource> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RoleResource::getId, roleResource.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部角色-资源关联
     * @param roleResource 角色-资源关联参数
     */
    @Override
    public Integer updateRoleResource(RoleResource roleResource) {
        validateUpdateId(roleResource.getId());
        return sqlExecuteResult(updateById(roleResource));
    }

    /**
     * 查询一条角色-资源关联
     * @param roleResourceId 角色-资源关联ID
     */
    @Override
    public RoleResource getRoleResourceById(Long roleResourceId) {
        LambdaQueryWrapper<RoleResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleResource::getId, roleResourceId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询角色-资源关联
     * @param roleResourceQueryDTO 角色-资源关联参数
     */
    @Override
    public Page<RoleResource> getRoleResourcePage(RoleResourceDTO.QueryDTO roleResourceQueryDTO) {
        Page<RoleResource> roleResourcePage = new Page<>(
                roleResourceQueryDTO.getPage(), roleResourceQueryDTO.getSize());
        LambdaQueryWrapper<RoleResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, roleResourceQueryDTO);

        BaseDeleted deleted = roleResourceQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(roleResourcePage, queryWrapper);
        } else {
            return page(roleResourcePage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 角色-资源关联 数量
     * @param roleResourceQueryDTO 角色-资源关联参数
     */
    @Override
    public Long getRoleResourceCount(RoleResourceDTO.QueryDTO roleResourceQueryDTO) {
        LambdaQueryWrapper<RoleResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, roleResourceQueryDTO);

        BaseDeleted deleted = roleResourceQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 角色-资源关联
     * @param roleResourceQueryDTO 角色-资源关联参数
     */
    @Override
    public List<RoleResource> getRoleResourceList(RoleResourceDTO.QueryDTO roleResourceQueryDTO) {
        LambdaQueryWrapper<RoleResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, roleResourceQueryDTO);

        BaseDeleted deleted = roleResourceQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<RoleResource> queryWrapper,
                              RoleResourceDTO.QueryDTO roleResourceQueryDTO) {

        Long roleId = roleResourceQueryDTO.getRoleId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(roleId), RoleResource::getRoleId, roleId);

        Long resourceId = roleResourceQueryDTO.getResourceId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(resourceId), RoleResource::getResourceId, resourceId);

        RoleResourcePermission permission = roleResourceQueryDTO.getPermission();
        queryWrapper.eq(ObjectUtils.isNotEmpty(permission), RoleResource::getPermission, permission);

        addQueryWrapper(queryWrapper, roleResourceQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<RoleResource, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                RoleResource::getOrderNumber,
                RoleResource::getTenantId,
                RoleResource::getApplicationId,
                RoleResource::getStatus,
                RoleResource::getDeleted,
                RoleResource::getDeleteUserId,
                RoleResource::getDeleteTime,
                RoleResource::getRemark,
                RoleResource::getCreateUserId,
                RoleResource::getCreateTime,
                RoleResource::getUpdateUserId,
                RoleResource::getUpdateTime,
                RoleResource::getExtend1,
                RoleResource::getExtend2,
                RoleResource::getExtend3);
    }

}
